package com.demo.service.impl;

import com.demo.common.Constants;
import com.demo.dao.ILogDao;
import com.demo.dao.INoticeDao;
import com.demo.dao.ISaleCashApplyDao;
import com.demo.entity.*;
import com.demo.entity.*;
import com.demo.service.ISaleCashApplyService;
import com.demo.service.ISalePersonAccountService;
import com.demo.service.ISalePersonService;
import com.demo.vo.SaleCashApplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by byte2 on 2017-03-26.
 */
@Transactional
@Service
public class SaleCashApplyImpl implements ISaleCashApplyService {
    @Autowired
    private ISaleCashApplyDao dao;
    @Autowired
    private ILogDao logDao; // 日志
    @Autowired
    private INoticeDao noticeDao; // 日志
    @Autowired
    private ISalePersonAccountService spaService;
    @Autowired
    private ISalePersonService spService;
//    private ISalePersonService spService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SaleCashApply record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(SaleCashApply record) {
        return dao.insertSelective(record);
    }

    /**
     * 申请提现
     *
     * @param record
     * @return
     */
    @Override
    public Object apply(SaleCashApply record, String carId) {
        SalePersonAccount spa = spaService.selectBySaleId(record.getSaleId());
        if (spa.getCashCanWithDrawTotal() < record.getApplyCount()) { //
            return "非法请求";
        } else {
            SalePerson sp = spService.selectByPrimaryKey(Integer.valueOf(record.getSaleId()));
            if (carId != null && !carId.equals(sp.getIdCard()) && "0".equals(sp.getValid())) { //第一次验证
                SalePerson sp1 = new SalePerson();
                sp1.setId(sp.getId());
                sp1.setIdCard(carId);
                spService.updateByPrimaryKeySelective(sp1);
            }
            spa.setCashCanWithDrawTotal(spa.getCashCanWithDrawTotal() - record.getApplyCount());
            spa.setCashWithDrawingTotal(spa.getCashWithDrawingTotal() + record.getApplyCount());
            spaService.updateByPrimaryKeySelective(spa);
            dao.insertSelective(record);
        }
        record.setNo(Constants.generateServiceNO("T"));
        // 提现日志
        Log log = new Log();
        log.setUserId(record.getSaleId());
        log.setContent("审核佣金提现" + record.getApplyCount().toString() + ",申请单号【" + record.getNo() + "】");
        logDao.insertSelective(log);
        return dao.insertSelective(record);
    }

    @Override
    public Object check(SaleCashApply record) {
        SaleCashApply sca = dao.selectByPrimaryKey(record.getId());
        if ((!"1".equals(record.getStatus()) && !"2".equals(record.getStatus())) || !"0".equals(sca.getStatus())) {
            return "非法请求";
        } else {
            if ("2".equals(record.getStatus())) {
                SalePersonAccount spa = spaService.selectBySaleId(sca.getSaleId());
                if (sca.getApplyCount() > spa.getCashWithDrawingTotal()) {
                    Log log = new Log();
                    log.setUserId(record.getSaleId());
                    log.setContent("【审核】操作非法：账户提现中金额小于本次提现金额!申请单号【" + sca.getNo() + "】!");
                    return "操作非法：账户提现中金额小于本次提现金额";
                } else {
                    spa.setCashWithDrawingTotal(spa.getCashWithDrawingTotal() - sca.getApplyCount());
                    spa.setCashCanWithDrawTotal(spa.getCashCanWithDrawTotal() + sca.getApplyCount());
                    Log log = new Log();
                    log.setUserId(record.getSaleId());
                    log.setContent("提现申请被拒：账户提现中金额退回可提现账户!申请单号【" + sca.getNo() + "】!");
                    spaService.updateByPrimaryKeySelective(spa);
                }
            } else if ("1".equals(record.getStatus())) {
                //申请通过，进入打款阶段
                //审核费用
                SalePersonAccount spa = spaService.selectBySaleId(sca.getSaleId());
                if (sca.getApplyCount() > spa.getCashWithDrawingTotal()) { //大于提现中金额
                    Log log = new Log();
                    log.setUserId(record.getSaleId());
                    log.setContent("【审核】操作非法：账户可提现金额小于本次提现金额!申请单号【" + sca.getNo() + "】!");
                    return "操作非法：账户可提现金额小于本次提现金额";
                }

            } else if ("3".equals(record.getStatus())) {
                //申请通过，进入打款阶段
                //审核费用
                SalePersonAccount spa = spaService.selectBySaleId(sca.getSaleId());
                if (sca.getApplyCount() > spa.getCashWithDrawingTotal()) { //大于提现中金额
                    Log log = new Log();
                    log.setUserId(record.getSaleId());
                    log.setContent("【审核】操作非法：账户可提现金额小于本次提现金额!申请单号【" + sca.getNo() + "】!");
                    return "操作非法：账户可提现金额小于本次提现金额";
                }else{
                    spa.setCashWithDrawingTotal(spa.getCashWithDrawingTotal() - sca.getApplyCount()); //销售佣金账户中实际扣款
                    spa.setCashCanWithDrawTotal(spa.getCashReceivedTotal() + sca.getApplyCount());
                    spaService.updateByPrimaryKeySelective(spa);
                }

            }
        }

        Notice notice = new Notice();
        if ("2".equals(record.getStatus())) {
            notice.setContent("【" + sca.getNo() + "】审核未通过:" + record.getRefuseReason());
        } else if ("1".equals(record.getStatus())) {
            notice.setContent("【" + sca.getNo() + "】审核通过");
        }
        noticeDao.insertSelective(notice);
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public Object pay(SaleCashApply record) {
        SaleCashApply sca = dao.selectByPrimaryKey(record.getId());
        if (!"3".equals(record.getStatus()) || !"1".equals(sca.getStatus())) {
            return "非法请求";
        } else {
            SalePersonAccount spa = spaService.selectBySaleId(sca.getSaleId());
            if (sca.getApplyCount() > spa.getCashWithDrawingTotal()) {
                Log log = new Log();
                log.setUserId(record.getSaleId());
                log.setContent("【支付】操作非法：账户提现中金额小于本次提现金额!申请单号【" + sca.getNo() + "】!");
                return "操作非法：账户提现中金额小于本次提现金额";
            } else {
                spa.setCashWithDrawingTotal(spa.getCashWithDrawingTotal() - sca.getApplyCount());
                spa.setCashReceivedTotal(spa.getCashReceivedTotal() + sca.getApplyCount());
                Log log = new Log();
                log.setUserId(record.getSaleId());
                log.setContent("提现打款：账户提现中金额进入已提现账户!申请单号【" + sca.getNo() + "】!");
                spaService.updateByPrimaryKeySelective(spa);
            }
        }
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public SaleCashApply selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public List<SaleCashApplyVo> queryByPage(SaleCashApplyVo scav) {
        return dao.queryByPage(scav);
    }

    @Override
    public int queryCountByPage(SaleCashApplyVo scav) {
        return dao.queryCountByPage(scav);
    }

    @Override
    public int updateByPrimaryKeySelective(SaleCashApply record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SaleCashApply record) {
        return dao.updateByPrimaryKey(record);
    }
}
